package homegroup.inc.utils;

import java.util.ArrayList;

public class PaginationState {
    private ArrayList<Integer> pageNumbers = new ArrayList<>();
    private int totalElements;
    private int elementsAmountPerPage;
    private int previousPage;
    private int nextPage;
    private int firstPage;
    private int lastPage;

    private int pagesAmountPerScreen;
    private int totalPages;
    private int currentPageNumber;

    private final int DEFAULT_PAGES_AMOUNT_PER_SCREEN = 5;

    public PaginationState(int totalElements) {
        this.totalElements = totalElements;
        init();
    }
    private void init() {
        elementsAmountPerPage = 12;
        totalPages = (totalElements - 1) / elementsAmountPerPage + 1;
        currentPageNumber = 1;
        pagesAmountPerScreen = DEFAULT_PAGES_AMOUNT_PER_SCREEN;
        if (DEFAULT_PAGES_AMOUNT_PER_SCREEN >= totalPages) {
            pagesAmountPerScreen = totalPages;
            firstPage = 0;
            lastPage = 0;
            previousPage = 0;
            nextPage = currentPageNumber + 1;
            if (totalPages == 1) {
                nextPage = 0;
            }
        }
        else {
            lastPage = totalPages;
            nextPage = currentPageNumber + 1;
        }
        for (int i = 1; i <= pagesAmountPerScreen; i++) {
            pageNumbers.add(i);
        }
    }
    public void display() {
        System.out.println("total elements = " + totalElements);
        System.out.println("total pages = " + totalPages);
        System.out.println("");
        System.out.print("prev = " + previousPage);
        System.out.print("    first element = " + firstPage + "   ");
        for (int pageNumber : pageNumbers){
            System.out.print(" " + pageNumber);
        }
        System.out.print("    last = " + lastPage);
        System.out.print("    next = " + nextPage);



    }



    public void changeState(int pageNumber) {
        /**
         * check inner page number
         * if page number are from range
         * set current page to page number
         */
        if (pageNumber > 0 && pageNumber <= totalPages) {
            currentPageNumber = pageNumber;
        }
        else {
            return;
        }
        /**
         *previous - next values
         */
        if (currentPageNumber < totalPages) {
            nextPage = currentPageNumber + 1;
            if (currentPageNumber == 1) {
                previousPage = 0;
            }
        }
        if (currentPageNumber > 1) {
            previousPage = currentPageNumber - 1;
            if (currentPageNumber == totalPages) {
                nextPage = 0;
            }
        }


        if (totalPages > DEFAULT_PAGES_AMOUNT_PER_SCREEN) {
            //////////////////////////////////////////////////

            int startNumber = currentPageNumber - 2;
            synchronized (pageNumbers) {
                pageNumbers.clear();
            }
            if (currentPageNumber <= 3) {
                startNumber = 1;
            }
            if (currentPageNumber > totalPages - 3) {
                startNumber = totalPages - pagesAmountPerScreen + 1;
            }
            /**
             * numbers with pages
             */
            for (int i = 0; i < pagesAmountPerScreen; i++ ) {
                pageNumbers.add(startNumber + i);
            }

            /**
             * first - last values
             */
            //////////////////////////////////////////////////
            if (currentPageNumber > 3) {
                firstPage = 1;
                if (currentPageNumber > totalPages - 3) {
                    lastPage = 0;
                }
            }
            if (currentPageNumber <= totalPages - 3) {
                lastPage = totalPages;
                if (currentPageNumber <= 3) {
                    firstPage = 0;
                }
            }
            //////////////////////////////////////////////////
        }

    }


    public int getLastPage() {
        return lastPage;
    }

    public int getFirstPage() {
        return firstPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getPreviousPage() {
        return previousPage;
    }

    public int getCurrentPageNumber() {
        return currentPageNumber;
    }

    public ArrayList<Integer> getPageNumbers() {
        return pageNumbers;
    }

    public int getElementsAmountPerPage() {
        return elementsAmountPerPage;
    }
}
